/**
 * 
 */
package net.kaizhong.j2ee01.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * http://localhost:8080/websocket/HelloWorld?name=huqifeng
 * 
 * @author kzuser
 *
 */
@WebServlet(name = "HelloWorld", urlPatterns = "/HelloWorld", initParams = {
        @WebInitParam(name = "locale", value = "zh_CN") }, loadOnStartup = 99)
public class HelloWorldServlet extends HttpServlet
{
    private static final Logger logger = LogManager.getLogger(HelloWorldServlet.class);

    /**
     * 
     */
    private static final long serialVersionUID = -1868095498876157432L;

    /**
     * 
     */
    public HelloWorldServlet()
    {
        logger.info("开始执行构造函数 HelloWorldServlet()");
        logger.info("结束执行构造函数 HelloWorldServlet()");
    }

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        logger.info("开始执行Servlet初始化 init()");
        super.init(config);
        logger.info("结束执行Servlet初始化 init()");
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        logger.info("开始执行 HelloWorldServlet.doGet()");
        this.doPost(req, resp);
        logger.info("结束执行 HelloWorldServlet.doGet()");
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        logger.info("开始执行 HelloWorldServlet.doPost()");
        String name = req.getParameter("name");
        if (null != name)
        {
            resp.getWriter().write("Hello World " + name + "!");
        }
        else
        {
            resp.getWriter().write("Hello World Guest!");
        }

        logger.info("结束执行 HelloWorldServlet.doPost()");
    }

    @Override
    public void destroy()
    {
        logger.info("开始执行Servlet销毁 HelloWorldServlet.destroy()");
        super.destroy();
        logger.info("结束执行Servlet销毁 HelloWorldServlet.destroy()");
    }
}
